#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"

/********************************************************************************************
********************************************************************************************
    ICN9608  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    ICN9608  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    ICN9608  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    ICN9608  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    ICN9608  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void ICN9608_Write_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		if(USB_Rx_Buffer[1]&0x80)
			SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		else
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void ICN9608_Write_51Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
	  //SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void ICN9608_Read_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
   SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0]=0x2D;	
		buffer[1]=0x02;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}

void ICN9608_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,m,k=0,temp;
	
		SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
		delay_ms(5);
	
    buffer1[0] = 0x03;        
    buffer1[1] = 0xF0;
    buffer1[2] = 0x5A;
    buffer1[3] = 0x5A;            //   Manufacture command set key      
    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	  delay_ms(5);  

		SSD2828_W_Reg(SigMode,channel,0xBC,30); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xBB); //GAMMA REG=>0XBB
		for( i = 0;i<29;i++)   //10个绑点
		{	                                                                                  
			SSD2828_W_Data(SigMode,channel,buffer[i+4]);	
			delay_ms(5);  
		}
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xBE;
    buffer1[2] = 0x01;    
    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	  delay_ms(5);  	
		
		buffer[4] = Uart_Error_None;
		buffer[1] =  0x08;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 		
		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void ICN9608_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u16 tmp;
    u8 i,j,k=0;
    //SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(10);
    buffer1[0] = 0x03;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x5A;
    buffer1[3] = 0x5A;                       // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    delay_ms(5);
    //SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);
		SSD2828_W_Reg(SigMode,channel,0xB7,(LP_B7_Data|0x0080));
    delay_ms(5);
				
		SSD2828_W_Reg(SigMode,channel,0xC1,0x001D); //返回个数设置
		buffer1[0] = 0x01;
		buffer1[1] = 0xBB;     		
		SSD2828_W_Array(SigMode,channel,buffer1,0);              
		delay_ms(5);  
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		delay_ms(5);
		for(i=0;i<29;i++)
		{
				SSD2828_W_Cmd(SigMode,channel,0xFA);		
				tmp=SPI3_Read_u16_Data(channel);
				buffer[4+i]=tmp>>8;
				buffer[5+i]=tmp;
				i++;
				delay_ms(5);
		}
    buffer[0]=0x2D;	//ICN9608
    buffer[1]=0x09;
		delay_ms(15);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  		

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void ICN9608_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[10];
	  u8 mtp_flag;
	  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15); 
    buffer1[0] = 0x03;
    buffer1[1] = 0xF1;
    buffer1[2] = 0x5A;
    buffer1[3] = 0x5A;                       //OTP_Register_Access_Enable
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 
    buffer1[0] = 0x02;
    buffer1[1] = 0xd2;
    buffer1[2] = USB_Rx_Buffer[4];  //OTP烧录项
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 

    buffer1[0] = 0x02;
    buffer1[1] = 0xD0;
    buffer1[2] = 0x04;    //OTP_MODE_Enable
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(10); 	
	
    buffer1[0] = 0x02;
    buffer1[1] = 0xD0;
    buffer1[2] = 0x05;    //OTP_MODE_Enable
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(120); 	

    buffer1[0] = 0x02;
    buffer1[1] = 0xD0;
    buffer1[2] = 0x04;    //OTP_Program_Enable
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(120); 	

    buffer1[0] = 0x02;
    buffer1[1] = 0xD0;
    buffer1[2] = 0x00;    //OTP_Program_End
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(10); 	
	
    buffer1[0] = 0x03;
    buffer1[1] = 0xF1;
    buffer1[2] = 0xA5;
    buffer1[3] = 0xA5;                       //OTP_Register_Access_Disable
    SSD2828_W_Array(SigMode,channel,buffer1,0); 	
		delay_ms(10);  	
		
		buffer[1] = 0x0A;
		buffer[2] = 0x02;
		buffer[3] = Uart_Error_None;   //返回 OTP Gamma数据  ：OK
		buffer[4] = 0;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);         
}


void ICN9608_Read_OTP_Times(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = 0xD8;                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);

		SSD2828_W_Cmd(SigMode,channel,0xFA);		
		buffer[4]=SPI3_Read_u8_Data(channel);
		delay_ms(5);

		buffer[0]=0x2D;	
		buffer[1]=0x0B;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}
/********************************************************************************************/


void ProcessForIc2D( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1]&0x0f)
	{
			case 0x01:                                      //写寄存器
				ICN9608_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x02:                                      //读寄存器
				ICN9608_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x03:                                      //写寄存器
				ICN9608_Write_51Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;															
			case 0x08:                                      //写Gamma数据    
				ICN9608_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x09:                                      //读Gamma数据
				ICN9608_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break; 
			case 0x0A://gamma otp
				ICN9608_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;
			case 0x0B://OTP times
				ICN9608_Read_OTP_Times(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;														
			default:	break;	
	}			
}


